﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityLayer;

namespace DataLayer.DAO
{
    public class clsUbigeoDAO
    {
        IEnumerable<Object> lista = null;

        private BD_LEGAJOEntities ContextoDeObjs = new BD_LEGAJOEntities();
        public bool Insertar(SGP_UBIGEO Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("SGP_UBIGEO", Obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool Modificar(SGP_UBIGEO obj)
        {

            try
            {
                ContextoDeObjs.AttachTo("SGP_UBIGEO", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        public bool Eliminar(SGP_UBIGEO obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("SGP_UBIGEO", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public List<SGP_UBIGEO> listar()
        {
            List<SGP_UBIGEO> lista = new List<SGP_UBIGEO>();
            var q = from c in ContextoDeObjs.SGP_UBIGEO
                    select c;
            lista = q.ToList();
            return lista;
        }
        public IEnumerable<Object> listarDepartamento(String IDDEPT = "")
        {
            lista = new List<Object>();
            try
            {
                var q = (from d in ContextoDeObjs.SGP_UBIGEO
                         select new
                         {
                             d.NDEP_SECDEPT,
                             d.CDEP_DESCRIP
                         }).Distinct();
                if (IDDEPT != String.Empty) q = q.Where(f1 => f1.NDEP_SECDEPT == IDDEPT);

                lista = q.ToList();
            }
            catch (Exception)
            {
                lista = null;
                throw;
            }

            return lista;
        }
        public IEnumerable<Object> listarProvincia(String IDDEPT, String IDPROV = "")
        {
            lista = new List<Object>();
            try
            {
                var q = (from p in ContextoDeObjs.SGP_UBIGEO
                         where p.NDEP_SECDEPT == IDDEPT
                         select new
                         {
                             p.NPROV_SECPROV,
                             p.CPROV_DESCRIP
                         }).Distinct();
                if (IDPROV != String.Empty) q = q.Where(f1 => f1.NPROV_SECPROV == IDPROV);

                lista = q.ToList();
            }
            catch (Exception)
            {
                lista = null;
                throw;
            }

            return lista;
        }
        public IEnumerable<Object> listarDistrito(String IDDEPT, String IDPROV, String IDDIST = "")
        {
            lista = new List<Object>();
            try
            {
                var q = (from d in ContextoDeObjs.SGP_UBIGEO
                         where d.NDEP_SECDEPT == IDDEPT
                         && d.NPROV_SECPROV == IDPROV
                         select new
                         {
                             d.NDIST_SECDIST,
                             d.CDIST_DESCRIP
                         }).Distinct();
                if (IDDIST != String.Empty) q = q.Where(f1 => f1.NDIST_SECDIST == IDDIST);

                lista = q.ToList();
            }
            catch (Exception)
            {
                lista = null;
                throw;
            }

            return lista;
        }
    }
}
